<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 订单
 */

namespace app\api\controller\user\v1_0\driver;


use think\Db;
class Order extends \app\api\controller\user\v1_0\Common
{
    protected $noNeedRight = ['test'];
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 取货
     * @return 
     * @Descripttion: 取货订单
     */    
    public function confirm(){
        $id = request()->param('id');
        $order = (new \app\common\model\DriverQOrder())->where([
            'id' => $id
        ])->find();
        // if($order['driver_state'] == 2){
        //     return self::errorResult("该笔订单已经处理完成！！！");
        // }
        Db::startTrans();
        try{
            $message = "订单创建失败,请重试";
            $process = true;

            $row = (new \app\common\model\DriverQOrder())->where(['id' => $id])->update([
                'state' => 2,
            ]);
            if(!$row){
                $message = "修改订单状态失败,请重试";
                $process = false;
            }
            #------生成送货司机订单
            $order_on = 'S' . date('YmdHis') . rand(1000, 9999);
            $driver = (new \app\common\model\Driver())->where(['s_bus_id' => $order['bus_id']])->find();
            $bus = (new \app\common\model\Bus())->where(['state' => 0,'id' => $order['bus_id']])->find();
            $row = (new \app\common\model\DriverSOrder())->insertGetId([
                'order_on' => $order_on,
                'goods_num' => $order['goods_num'],
                'state' => 1,
                'start_address' => '',
                'end_address' =>  $bus['address'],
                'agent_id' => $order['agent_id'],
                'addtime' => time(),
                'batch' => date("Ymd"),
                'driver_id' => $driver['id'],
                'bus_id' => $order['bus_id'],
                'end_latitude' => $bus['latitude'],
                'end_longitude' => $bus['longitude'],
              ]);
              if(!$row){
                $process = false;
                $message = "订单创建失败1,添加记录失败";
              }
            
              $row = (new \app\common\model\UserOrders())
                  ->where(['q_order_id' => $order['id'],'driver_state' => 0])
                  ->update([
                    's_order_id' => $row,
                    'driver_id' => $driver['id'],
                    'driver_state' => 1,
                    'driver_q_state' => 2,
                  ]);
              if(!$row){
                $process = false;
                $message = "订单创建失败2,修改订单信息失败";
              }
            if ($process !== false) {
                Db::commit(); // 提交事务
            } else {
                Db::rollback(); // 回滚事务
            }
         } catch (\Exception $e) {
            $process = false;
            $message = $e->getMessage();
            Db::rollback(); // 回滚事务
        }
        if ($process !== false) {
            return self::successResult("成功");
        } else {
            return self::errorResult($message);
        }

    }
     /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 送货
     * @return 
     * @Descripttion: 送货订单
     */    
    public function s_confirm(){
        $id = request()->param('id');
        $order = (new \app\common\model\DriverSOrder())->where([
            'id' => $id
        ])->find();
        // if($order['driver_state'] == 2){
        //     return self::errorResult("该笔订单已经处理完成！！！");
        // }
        Db::startTrans();
        try{
            $message = "订单创建失败,请重试";
            $process = true;

            $row = (new \app\common\model\DriverSOrder())->where(['id' => $id])->update([
                'state' => 2,
            ]);
            if(!$row){
                $message = "修改订单状态失败1,请重试";
                $process = false;
            }
            $bus = (new \app\common\model\Bus())->where(['state' => 0,'id' => $order['bus_id']])->find();
            $order_on = 'T' . date('YmdHis') . rand(1000, 9999);
            $row = (new \app\common\model\BusOrder())->insertGetId([
                'order_on' => $order_on,
                'goods_num' => $order['goods_num'],
                'state' => 1,
                'start_address' => '',
                'end_address' =>  $bus['address'],
                'agent_id' => $order['agent_id'],
                'addtime' => time(),
                'batch' => date("Ymd"),
                'bus_id' => $order['bus_id'],
                'end_latitude' => $bus['latitude'],
                'end_longitude' => $bus['longitude'],
              ]);
              if(!$row){
                $process = false;
                $message = "订单创建失败2,添加记录失败";
              }
            $row = (new \app\common\model\UserOrders())->where([
                's_order_id' => $id,
                'bus_state' => 0
                ])->update([
                'driver_state' => 2,
                'bus_state' => 1,
                'bus_order_id' => $row,
            ]);
            if(!$row){
                $message = "修改订单状态失败3,请重试";
                $process = false;
            }
            // $row = (new \app\common\model\BusOrder())->where(['id' => $id])->update([
            //     'state' => 1,
            // ]);
            // if(!$row){
            //     $message = "修改订单状态失败,请重试";
            //     $process = false;
            // }

            if ($process !== false) {
                Db::commit(); // 提交事务
            } else {
                Db::rollback(); // 回滚事务
            }
         } catch (\Exception $e) {
            $process = false;
            $message = $e->getMessage();
            Db::rollback(); // 回滚事务
        }
        if ($process !== false) {
            return self::successResult("成功");
        } else {
            return self::errorResult($message);
        }

    }

    
    

}
